﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Product_Detail
    Inherits System.Web.UI.UserControl
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then

            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            If Not Request.QueryString("Productid").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Productid")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Product = (From q In Data.Products Where q.ProductId = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                lblTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                lblDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                lblChiTietSanPham.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                imgImagePreview.ImageUrl = IIf(IsNothing(p.ImagePreview) = True, "", p.ImagePreview)
                lblOriginalprice.Text = IIf(IsNothing(p.OriginalPrice) = True, "", FormatNumber(p.OriginalPrice))
                'lay thong tin nhan hieu
                Dim _nhanhieu = (From q In data.ProductUnits Where q.ProductUnitID = p.ProductUnitId Select q).FirstOrDefault
                If Not p.ProductUnitId = "0" Then
                    lblNhanHieu.Text = _nhanhieu.ProductUnitName
                Else
                    lblNhanHieu.Text = ""
                End If

                'lay thong tin size
                Dim _arrSize = (From q In data.ProductSizes Where q.ProductId = p.ProductId Select q.SizeId).ToArray
                Dim _lstSize = (From q In data.ProSizes Where _arrSize.Contains(q.Id) Select q Order By q.Title).ToList
                With rptSize
                    .DataSource = _lstSize
                    .DataBind()
                End With
                'end lay thong tin size
                'lay thong tin mau
                Dim _arrColor = (From q In data.ProductColors Where q.ProductId = p.ProductId Select q.ColorId).ToArray
                Dim _lstColor = (From q In data.ProColors Where _arrColor.Contains(q.Id) Select q Order By q.Title).ToList
                With rptColor
                    .DataSource = _lstColor
                    .DataBind()
                End With
                'end lay thong tin smau
                'lay thong tin mau
                Dim _arrDoTuoi = (From q In data.ProductDoTuois Where q.ProductId = p.ProductId Select q.DoTuoiId).ToArray
                Dim _lstDoTuoi = (From q In data.ProDoTuois Where _arrDoTuoi.Contains(q.Id) Select q Order By q.Title).ToList
                With rptDoTuoi
                    .DataSource = _lstDoTuoi
                    .DataBind()
                End With
                'end lay thong tin smau
                'lay Images
                Dim _arrAnh = (From q In data.ProductImages Where q.ProductId = p.ProductId Select q.Id).ToArray
                Dim _lstAnh = (From q In data.ProductImages Where _arrAnh.Contains(q.Id) Select q Order By q.ImageURL).ToList
                With rptImages
                    .DataSource = _lstAnh
                    .DataBind()
                End With
                'end lay Images
            End If
        End Using
    End Sub

#End Region

    Protected Sub rptSize_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptSize.ItemDataBound
        Dim pros = CType(e.Item.DataItem, ProSize)
        Dim ltrSize As Literal = CType(e.Item.FindControl("ltrSize"), Literal)
        ltrSize.Text = pros.Title

    End Sub

    Protected Sub rptColor_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptColor.ItemDataBound
        Dim proc = CType(e.Item.DataItem, ProColor)
        Dim ltrColor As Literal = CType(e.Item.FindControl("ltrColor"), Literal)
        ltrColor.Text = proc.Title
    End Sub

    Protected Sub rptDoTuoi_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptDoTuoi.ItemDataBound
        Dim prodt = CType(e.Item.DataItem, ProDoTuoi)
        Dim ltrDoTuoi As Literal = CType(e.Item.FindControl("ltrDoTuoi"), Literal)
        ltrDoTuoi.Text = prodt.Title
    End Sub

    Protected Sub rptImages_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImages.ItemDataBound
        Dim proi = CType(e.Item.DataItem, ProductImage)
        Dim ltrImages As Image = CType(e.Item.FindControl("ltrImages"), Image)
        ltrImages.ImageUrl = proi.ImageURL

    End Sub
End Class
